package com.piece.mall.service;

import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.object.ObjectUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.file.client.FileClientFactory;
import com.piece.file.model.TbSysFile;
import com.piece.file.service.TbSysFileService;
import com.piece.mall.model.PmsSpuInfoDesc;
import com.piece.mall.repository.PmsSpuInfoDescRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
@Transactional
public class PmsSpuInfoDescService extends BaseService<PmsSpuInfoDesc, String> {

    @Autowired
    private PmsSpuInfoDescRepository pmsSpuInfoDescRepository;

    @Resource
    private TbSysFileService tbSysFileService;

    @Autowired
    private FileClientFactory fileClientFactory;

    @PostConstruct
    public void initRepository() {
        setRepository(pmsSpuInfoDescRepository);
    }

    @Override
    public PmsSpuInfoDesc insert(PmsSpuInfoDesc desc) {
        String[] images = Convert.toStrArray(desc.getIntroduce());
        if (null != images) {
            // 维护文件表关联数据
            List<TbSysFile> files = new ArrayList<>();
            Arrays.stream(images).forEach(id -> {
                TbSysFile file = tbSysFileService.findById(id);
                if (null != file) {
                    file.setTable(PmsSpuInfoDesc.class.getSimpleName());
                    file.setBusinessId(desc.getSpuId() + "");
                    files.add(file);
                }
            });
            if (ObjectUtil.isNotEmpty(files)) {
                try {
                    tbSysFileService.batchUpdate(files);
                } catch (Exception e) {
                    log.error("批量保存文件异常：{}", e.getMessage());
                }
            }
        }
        return pmsSpuInfoDescRepository.insert(desc);
    }

    @Override
    public void delete(Serializable spuId) {
        PmsSpuInfoDesc desc = pmsSpuInfoDescRepository.findById(spuId);
        if (null != desc && StringUtil.isNotEmpty(desc.getIntroduce())) {
            String[] deleteFiles = Convert.toStrArray(desc.getIntroduce());
            if (null != deleteFiles) {
                try {
                    tbSysFileService.deleteByTableAndBusinessList(PmsSpuInfoDesc.class.getSimpleName(), Arrays.asList(deleteFiles));
                } catch (Exception e) {
                    log.error("删除文件失败", e);
                }
            }
        }
    }

    @Override
    public int delete(Serializable[] spuIds) {
        Arrays.stream(spuIds).forEach(spuId -> delete(spuId));
        return spuIds.length;
    }
}
